package com.ycj.design.structure.proxy;

import org.junit.Test;

import java.lang.reflect.Proxy;
import java.time.Duration;
import java.time.Instant;

/**
 * @author xiyang.ycj
 * @since Nov 21, 2019 18:07:05 PM
 */
public class ProxyClient {

    @Test
    public void staticProxy(){
        // 创建一个买家
        IBuyer buyer = new Buyer();
        // 找到一个中介
        IBuyer proxyBuyer = new ProxyBuyer(buyer);
        proxyBuyer.buy();
    }

    @Test
    public void dynamicProxy(){
        Instant start = Instant.now();
        // 创建一个买家
        IBuyer buyer = new Buyer();
        // 找到一个中介
        IBuyer instance = (IBuyer)Proxy.newProxyInstance(Buyer.class.getClassLoader(), new Class[]{IBuyer.class}, new Dynamic(buyer));
        instance.buy();
        Instant end = Instant.now();
        System.out.println(String.format("耗时%sms", Duration.between(start,end).toMillis()));
    }

    @Test
    public void cglibProxy(){
        Instant start = Instant.now();
        Buyer instance = new Cglib().getInstance(Buyer.class);
        instance.buy();
        Instant end = Instant.now();
        System.out.println(String.format("耗时%sms", Duration.between(start,end).toMillis()));
    }
}
